/**
 * 存储工具函数
 */

// 存储前缀
const STORAGE_PREFIX = 'Z_AShopping_';

/**
 * 存储数据
 * @param {String} key 键名
 * @param {*} value 值
 * @param {Boolean} usePrefix 是否使用前缀
 */
export const setStorage = (key, value, usePrefix = true) => {
  const storageKey = usePrefix ? `${STORAGE_PREFIX}${key}` : key;
  try {
    const storageValue = typeof value === 'object' ? JSON.stringify(value) : value;
    uni.setStorageSync(storageKey, storageValue);
  } catch (error) {
    console.error('存储数据失败:', error);
  }
};

/**
 * 获取数据
 * @param {String} key 键名
 * @param {*} defaultValue 默认值
 * @param {Boolean} usePrefix 是否使用前缀
 * @returns {*} 存储的值
 */
export const getStorage = (key, defaultValue = null, usePrefix = true) => {
  const storageKey = usePrefix ? `${STORAGE_PREFIX}${key}` : key;
  try {
    const value = uni.getStorageSync(storageKey);
    if (value === '' || value === undefined || value === null) {
      return defaultValue;
    }
    try {
      return JSON.parse(value);
    } catch (error) {
      return value;
    }
  } catch (error) {
    console.error('获取数据失败:', error);
    return defaultValue;
  }
};

/**
 * 移除数据
 * @param {String} key 键名
 * @param {Boolean} usePrefix 是否使用前缀
 */
export const removeStorage = (key, usePrefix = true) => {
  const storageKey = usePrefix ? `${STORAGE_PREFIX}${key}` : key;
  try {
    uni.removeStorageSync(storageKey);
  } catch (error) {
    console.error('移除数据失败:', error);
  }
};

/**
 * 清空所有数据
 * @param {Boolean} onlyPrefix 是否只清空带前缀的数据
 */
export const clearStorage = (onlyPrefix = true) => {
  try {
    if (onlyPrefix) {
      const storageInfo = uni.getStorageInfoSync();
      const keys = storageInfo.keys;
      keys.forEach(key => {
        if (key.startsWith(STORAGE_PREFIX)) {
          uni.removeStorageSync(key);
        }
      });
    } else {
      uni.clearStorageSync();
    }
  } catch (error) {
    console.error('清空数据失败:', error);
  }
};

/**
 * 获取所有数据
 * @param {Boolean} onlyPrefix 是否只获取带前缀的数据
 * @returns {Object} 所有数据
 */
export const getAllStorage = (onlyPrefix = true) => {
  try {
    const storageInfo = uni.getStorageInfoSync();
    const keys = storageInfo.keys;
    const result = {};
    
    keys.forEach(key => {
      if (!onlyPrefix || key.startsWith(STORAGE_PREFIX)) {
        const realKey = onlyPrefix ? key.substring(STORAGE_PREFIX.length) : key;
        result[realKey] = getStorage(realKey, null, onlyPrefix);
      }
    });
    
    return result;
  } catch (error) {
    console.error('获取所有数据失败:', error);
    return {};
  }
};

/**
 * 获取用户信息
 */
export const getUserInfo = () => {
  return getStorage('userInfo', null);
};

/**
 * 设置用户信息
 * @param {Object} userInfo 用户信息
 */
export const setUserInfo = (userInfo) => {
  setStorage('userInfo', userInfo);
};

/**
 * 获取Token
 */
export const getToken = () => {
  return getStorage('token', '');
};

/**
 * 设置Token
 * @param {String} token Token
 */
export const setToken = (token) => {
  setStorage('token', token);
};

/**
 * 清除用户信息和Token
 */
export const clearUserData = () => {
  removeStorage('userInfo');
  removeStorage('token');
}; 